#include<bits/stdc++.h>
using namespace std;
const int N = 1000+7;
int f[N][N], w[N], v[N];
int t, m;
int main() {
  cin>>t>>m;
  for(int i=1; i<=m; ++i) cin>>w[i]>>v[i];
  for(int i=1; i<=m; ++i) {
    for(int j=1; j<=t; ++j) {
      if(j-w[i]>=0)
        f[i][j] = max(f[i-1][j], f[i-1][j-w[i]]+v[i]);
      else f[i][j] = f[i-1][j];
    }
  }
  cout<<f[m][t]<<'\n';
}
